package com.always.electrical.energy.web.service;

import com.always.electrical.energy.web.data.entity.CjDydlqx;
import com.always.electrical.energy.web.data.entity.CjGlqx;
import com.always.electrical.energy.web.data.entity.IotMcuReposrtData;
import com.always.electrical.energy.web.data.entity.IotPcsReportData;
import com.always.electrical.energy.web.req.*;
import com.always.electrical.energy.web.res.*;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.async.DeferredResult;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 微网系统设置【柔直互联】
 */
@Service
public class MicrogridService extends BaseService {

    // 阈值调控 正向重过载阈值 召测
    public void thresholdPositiveGet(DeferredResult<WebBaseResponse> result, List<BigDecimal> deviceIds) {
        if (deviceIds.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, BigDecimal> deviceIdTmlIdMap = getDeviceIdTmlIdMap(deviceIds);

        AtomicInteger countLatch = new AtomicInteger(deviceIds.size());
        MicrogridThresholdPositiveRes webRes = new MicrogridThresholdPositiveRes();

        for (BigDecimal deviceId : deviceIds) {
            MicrogridThresholdPositiveRes.Item webResItem = new MicrogridThresholdPositiveRes.Item().setId(deviceId);
            webRes.getItems().add(webResItem);
            BigDecimal tmlId = deviceIdTmlIdMap.get(deviceId);
            if (tmlId == null) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendThresholdPositiveReq(tmlId, deviceId, "").setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setPositive(res.getPositive());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }

    // 阈值调控 正向重过载阈值 设置
    public void thresholdPositiveSet(DeferredResult<WebBaseResponse> result, List<MicrogridThresholdPositiveReq.Item> items) {
        if (items.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, BigDecimal> deviceIdTmlIdMap = getDeviceIdTmlIdMap(items.stream().map(MicrogridThresholdPositiveReq.Item::getId).collect(Collectors.toList()));

        AtomicInteger countLatch = new AtomicInteger(items.size());
        MicrogridThresholdPositiveRes webRes = new MicrogridThresholdPositiveRes();

        for (MicrogridThresholdPositiveReq.Item item : items) {
            MicrogridThresholdPositiveRes.Item webResItem = new MicrogridThresholdPositiveRes.Item().setId(item.getId());
            webRes.getItems().add(webResItem);
            BigDecimal tmlId = deviceIdTmlIdMap.get(item.getId());
            if (tmlId == null) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendThresholdPositiveReq(tmlId, item.getId(), item.getPositive()).setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setPositive(res.getPositive());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }

    // 阈值调控 反向重过载阈值 召测
    public void thresholdNegativeGet(DeferredResult<WebBaseResponse> result, List<BigDecimal> deviceIds) {
        if (deviceIds.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, BigDecimal> deviceIdTmlIdMap = getDeviceIdTmlIdMap(deviceIds);

        AtomicInteger countLatch = new AtomicInteger(deviceIds.size());
        MicrogridThresholdNegativeRes webRes = new MicrogridThresholdNegativeRes();

        for (BigDecimal deviceId : deviceIds) {
            MicrogridThresholdNegativeRes.Item webResItem = new MicrogridThresholdNegativeRes.Item().setId(deviceId);
            webRes.getItems().add(webResItem);
            BigDecimal tmlId = deviceIdTmlIdMap.get(deviceId);
            if (tmlId == null) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendThresholdNegativeReq(tmlId, deviceId, "").setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setNegative(res.getNegative());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }

    // 阈值调控 反向重过载阈值 设置
    public void thresholdNegativeSet(DeferredResult<WebBaseResponse> result, List<MicrogridThresholdNegativeReq.Item> items) {
        if (items.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, BigDecimal> deviceIdTmlIdMap = getDeviceIdTmlIdMap(items.stream().map(MicrogridThresholdNegativeReq.Item::getId).collect(Collectors.toList()));

        AtomicInteger countLatch = new AtomicInteger(items.size());
        MicrogridThresholdNegativeRes webRes = new MicrogridThresholdNegativeRes();

        for (MicrogridThresholdNegativeReq.Item item : items) {
            MicrogridThresholdNegativeRes.Item webResItem = new MicrogridThresholdNegativeRes.Item().setId(item.getId());
            webRes.getItems().add(webResItem);
            BigDecimal tmlId = deviceIdTmlIdMap.get(item.getId());
            if (tmlId == null) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendThresholdNegativeReq(tmlId, item.getId(), item.getNegative()).setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setNegative(res.getNegative());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }

    // 阈值调控 储能充电阈值 召测
    public void thresholdChargeGet(DeferredResult<WebBaseResponse> result, List<BigDecimal> deviceIds) {
        if (deviceIds.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, BigDecimal> deviceIdTmlIdMap = getDeviceIdTmlIdMap(deviceIds);

        AtomicInteger countLatch = new AtomicInteger(deviceIds.size());
        MicrogridThresholdChargeRes webRes = new MicrogridThresholdChargeRes();

        for (BigDecimal deviceId : deviceIds) {
            MicrogridThresholdChargeRes.Item webResItem = new MicrogridThresholdChargeRes.Item().setId(deviceId);
            webRes.getItems().add(webResItem);
            BigDecimal tmlId = deviceIdTmlIdMap.get(deviceId);
            if (tmlId == null) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendThresholdChargeReq(tmlId, deviceId, "").setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setCharge(res.getCharge());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }

    // 阈值调控 储能充电阈值 设置
    public void thresholdChargeSet(DeferredResult<WebBaseResponse> result, List<MicrogridThresholdChargeReq.Item> items) {
        if (items.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, BigDecimal> deviceIdTmlIdMap = getDeviceIdTmlIdMap(items.stream().map(MicrogridThresholdChargeReq.Item::getId).collect(Collectors.toList()));

        AtomicInteger countLatch = new AtomicInteger(items.size());
        MicrogridThresholdChargeRes webRes = new MicrogridThresholdChargeRes();

        for (MicrogridThresholdChargeReq.Item item : items) {
            MicrogridThresholdChargeRes.Item webResItem = new MicrogridThresholdChargeRes.Item().setId(item.getId());
            webRes.getItems().add(webResItem);
            BigDecimal tmlId = deviceIdTmlIdMap.get(item.getId());
            if (tmlId == null) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendThresholdChargeReq(tmlId, item.getId(), item.getCharge()).setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setCharge(res.getCharge());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }

    // 阈值调控 储能放电阈值 召测
    public void thresholdDisChargeGet(DeferredResult<WebBaseResponse> result, List<BigDecimal> deviceIds) {
        if (deviceIds.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, BigDecimal> deviceIdTmlIdMap = getDeviceIdTmlIdMap(deviceIds);

        AtomicInteger countLatch = new AtomicInteger(deviceIds.size());
        MicrogridThresholdDisChargeRes webRes = new MicrogridThresholdDisChargeRes();

        for (BigDecimal deviceId : deviceIds) {
            MicrogridThresholdDisChargeRes.Item webResItem = new MicrogridThresholdDisChargeRes.Item().setId(deviceId);
            webRes.getItems().add(webResItem);
            BigDecimal tmlId = deviceIdTmlIdMap.get(deviceId);
            if (tmlId == null) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendThresholdDisChargeReq(tmlId, deviceId, "").setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setDischarge(res.getDischarge());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }

    // 阈值调控 储能放电阈值 设置
    public void thresholdDisChargeSet(DeferredResult<WebBaseResponse> result, List<MicrogridThresholdDisChargeReq.Item> items) {
        if (items.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, BigDecimal> deviceIdTmlIdMap = getDeviceIdTmlIdMap(items.stream().map(MicrogridThresholdDisChargeReq.Item::getId).collect(Collectors.toList()));

        AtomicInteger countLatch = new AtomicInteger(items.size());
        MicrogridThresholdDisChargeRes webRes = new MicrogridThresholdDisChargeRes();

        for (MicrogridThresholdDisChargeReq.Item item : items) {
            MicrogridThresholdDisChargeRes.Item webResItem = new MicrogridThresholdDisChargeRes.Item().setId(item.getId());
            webRes.getItems().add(webResItem);
            BigDecimal tmlId = deviceIdTmlIdMap.get(item.getId());
            if (tmlId == null) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendThresholdDisChargeReq(tmlId, item.getId(), item.getDischarge()).setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setDischarge(res.getDischarge());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }

    // 计划并离网 查询
    public MicrogridGridStOpRes gridStOpGet(List<BigDecimal> deviceIds) {
        MicrogridGridStOpRes webRes = new MicrogridGridStOpRes();
        if (!deviceIds.isEmpty()) {
            Map<BigDecimal, String> iotMcuReposrtDataMap = iotMcuReposrtDataService.lambdaQuery()
                    .in(IotMcuReposrtData::getMcuId, deviceIds)
                    .list()
                    .stream().collect(Collectors.toMap(IotMcuReposrtData::getMcuId, IotMcuReposrtData::getGridst));

            for (BigDecimal deviceId : deviceIds) {
                webRes.getItems().add(new MicrogridGridStOpRes.Item().setId(deviceId).setOp(iotMcuReposrtDataMap.get(deviceId)));
            }
        }
        return webRes;
    }

    // 计划并离网 设置
    public void gridStOpSet(DeferredResult<WebBaseResponse> result, List<MicrogridGridStOpReq.Item> items) {
        if (items.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, BigDecimal> deviceIdTmlIdMap = getDeviceIdTmlIdMap(items.stream().map(MicrogridGridStOpReq.Item::getId).collect(Collectors.toList()));

        AtomicInteger countLatch = new AtomicInteger(items.size());
        MicrogridGridStOpRes webRes = new MicrogridGridStOpRes();

        for (MicrogridGridStOpReq.Item item : items) {
            MicrogridGridStOpRes.Item webResItem = new MicrogridGridStOpRes.Item().setId(item.getId());
            webRes.getItems().add(webResItem);
            BigDecimal tmlId = deviceIdTmlIdMap.get(item.getId());
            if (tmlId == null) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendMicrogridGridStOpSetReq(tmlId, item.getId(), item.getOp()).setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setOp(res.getOp());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }

    // PCS 运行状态 召测
    public WebResponse pcsRunningStateGet(List<BigDecimal> deviceIds) {

        MicrogridPcsRunningStateRes webRes = new MicrogridPcsRunningStateRes();

        if (!deviceIds.isEmpty()) {
            Map<BigDecimal, IotPcsReportData> iotPcsReportDataMap =  iotPcsReportDataService.selectNewList(deviceIds)
                    .stream().collect(Collectors.toMap(IotPcsReportData::getPcsId, e -> e));

            for (BigDecimal deviceId : deviceIds) {
                MicrogridPcsRunningStateRes.Item webResItem = new MicrogridPcsRunningStateRes.Item().setId(deviceId);

                IotPcsReportData iotPcsReportData = iotPcsReportDataMap.get(deviceId);
                if (iotPcsReportData != null) {
                    webResItem.setRunningState(iotPcsReportData.getRunst());
                }

                webRes.getItems().add(webResItem);
            }
        }
        return webRes;
    }

    // PCS 整机状态 召测
    public WebResponse pcsMachineStateGet(List<BigDecimal> deviceIds) {

        MicrogridPcsMachineStateRes webRes = new MicrogridPcsMachineStateRes();

        if (!deviceIds.isEmpty()) {
            Map<BigDecimal, IotPcsReportData> iotPcsReportDataMap =  iotPcsReportDataService.selectNewList(deviceIds)
                    .stream().collect(Collectors.toMap(IotPcsReportData::getPcsId, e -> e));

            for (BigDecimal deviceId : deviceIds) {
                MicrogridPcsMachineStateRes.Item webResItem = new MicrogridPcsMachineStateRes.Item().setId(deviceId);

                IotPcsReportData iotPcsReportData = iotPcsReportDataMap.get(deviceId);
                if (iotPcsReportData != null) {
                    webResItem.setMachineState(iotPcsReportData.getRunmodop());
                }

                webRes.getItems().add(webResItem);
            }
        }
        return webRes;
    }

    // PCS 运行模式 设置
    public void pcsRunningModeSet(DeferredResult<WebBaseResponse> result, List<MicrogridPcsRunningModeReq.Item> items) {
        if (items.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, String> deviceIdNodeIdMap = getDeviceIdNodeIdMap(items.stream().map(MicrogridPcsRunningModeReq.Item::getId).collect(Collectors.toList()));

        AtomicInteger countLatch = new AtomicInteger(items.size());
        MicrogridPcsRunningModeRes webRes = new MicrogridPcsRunningModeRes();

        for (MicrogridPcsRunningModeReq.Item item : items) {
            MicrogridPcsRunningModeRes.Item webResItem = new MicrogridPcsRunningModeRes.Item().setId(item.getId());
            webRes.getItems().add(webResItem);
            String nodeId = deviceIdNodeIdMap.get(item.getId());
            if (nodeId == null || nodeId.isEmpty()) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendPcsRunningStateModeReq(nodeId, item.getRunningMode()).setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setRunningMode(res.getRunningStateMode());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }

    // PCS 整机状态 设置
    public void pcsMachineStateSet(DeferredResult<WebBaseResponse> result, List<MicrogridPcsMachineStateReq.Item> items) {
        if (items.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, String> deviceIdNodeIdMap = getDeviceIdNodeIdMap(items.stream().map(MicrogridPcsMachineStateReq.Item::getId).collect(Collectors.toList()));

        AtomicInteger countLatch = new AtomicInteger(items.size());
        MicrogridPcsMachineStateRes webRes = new MicrogridPcsMachineStateRes();

        for (MicrogridPcsMachineStateReq.Item item : items) {
            MicrogridPcsMachineStateRes.Item webResItem = new MicrogridPcsMachineStateRes.Item().setId(item.getId());
            webRes.getItems().add(webResItem);
            String nodeId = deviceIdNodeIdMap.get(item.getId());
            if (nodeId == null || nodeId.isEmpty()) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendPcsStorageMachineStateReq(nodeId, item.getMachineState()).setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setMachineState(res.getMachineState());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }

    // PCS 台区负载率
    public MicrogridPcsRateRes pcsRate(List<BigDecimal> deviceIds) {
        MicrogridPcsRateRes webRes = new MicrogridPcsRateRes();
        if (!deviceIds.isEmpty()) {
            Map<BigDecimal, BigDecimal> cjGlqxMap = cjGlqxService.selectNewList(deviceIds)
                    .stream()
                    .filter(item -> item.getZyggl() != null)
                    .collect(Collectors.toMap(CjGlqx::getYxdnbid, CjGlqx::getZyggl));

            for (BigDecimal deviceId : deviceIds) {
                webRes.getItems().add(new MicrogridPcsRateRes.Item().setId(deviceId).setActivePower(cjGlqxMap.get(deviceId)));
            }
        }
        return webRes;
    }

    // PCS 直流侧电压 召测
    public WebResponse pcsDc(List<BigDecimal> deviceIds) {
        MicrogridPcsDcRes webRes = new MicrogridPcsDcRes();

        if (!deviceIds.isEmpty()) {
            Map<BigDecimal, IotPcsReportData> iotPcsReportDataMap =  iotPcsReportDataService.selectNewList(deviceIds)
                    .stream().collect(Collectors.toMap(IotPcsReportData::getPcsId, e -> e));

            for (BigDecimal deviceId : deviceIds) {
                MicrogridPcsDcRes.Item webResItem = new MicrogridPcsDcRes.Item().setId(deviceId);

                IotPcsReportData iotPcsReportData = iotPcsReportDataMap.get(deviceId);
                if (iotPcsReportData != null) {
                    webResItem.setDc(String.valueOf(iotPcsReportData.getDcTotv()));
                }

                webRes.getItems().add(webResItem);
            }
        }
        return webRes;
    }

    // PCS 交流侧电压 查询
    public WebResponse pcsAc(List<BigDecimal> deviceIds) {
        MicrogridPcsAcRes webRes = new MicrogridPcsAcRes();

        Map<BigDecimal, CjDydlqx> cjDydlqxMap = cjDydlqxService.selectNewList(deviceIds)
                .stream().collect(Collectors.toMap(CjDydlqx::getYxdnbid, e -> e));

        if (!deviceIds.isEmpty()) {
            for (BigDecimal deviceId : deviceIds) {
                MicrogridPcsAcRes.Item item = new MicrogridPcsAcRes.Item();

                CjDydlqx cjDydlqx = cjDydlqxMap.get(deviceId);
                if (cjDydlqx != null) {
                    item.setAc(cjDydlqx.getBdy());
                }

                item.setId(deviceId);
                webRes.getItems().add(item);
            }
        }
        return webRes;
    }

    // 储能 运行状态 召测
    public void storageRunningStateGet(DeferredResult<WebBaseResponse> result, List<BigDecimal> deviceIds) {
        if (deviceIds.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, String> deviceIdNodeIdMap = getDeviceIdNodeIdMap(deviceIds);

        AtomicInteger countLatch = new AtomicInteger(deviceIds.size());
        MicrogridStorageRunningStateRes webRes = new MicrogridStorageRunningStateRes();

        for (BigDecimal deviceId : deviceIds) {
            MicrogridStorageRunningStateRes.Item webResItem = new MicrogridStorageRunningStateRes.Item().setId(deviceId);
            webRes.getItems().add(webResItem);
            String nodeId = deviceIdNodeIdMap.get(deviceId);
            if (nodeId == null || nodeId.isEmpty()) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendPcsStorageRunningStateGetReq(nodeId, "").setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setRunningState(res.getRunningState());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }

    // 储能 整机状态 召测
    public void storageMachineStateGet(DeferredResult<WebBaseResponse> result, List<BigDecimal> deviceIds) {
        if (deviceIds.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, String> deviceIdNodeIdMap = getDeviceIdNodeIdMap(deviceIds);

        AtomicInteger countLatch = new AtomicInteger(deviceIds.size());
        MicrogridStorageMachineStateRes webRes = new MicrogridStorageMachineStateRes();

        for (BigDecimal deviceId : deviceIds) {
            MicrogridStorageMachineStateRes.Item webResItem = new MicrogridStorageMachineStateRes.Item().setId(deviceId);
            webRes.getItems().add(webResItem);
            String nodeId = deviceIdNodeIdMap.get(deviceId);
            if (nodeId == null || nodeId.isEmpty()) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendPcsStorageMachineStateReq(nodeId, "").setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setMachineState(res.getMachineState());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }

    // 储能 运行模式 设置
    public void storageRunningModeSet(DeferredResult<WebBaseResponse> result, List<MicrogridStorageRunningModeReq.Item> items) {
        if (items.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, String> deviceIdNodeIdMap = getDeviceIdNodeIdMap(items.stream().map(MicrogridStorageRunningModeReq.Item::getId).collect(Collectors.toList()));

        AtomicInteger countLatch = new AtomicInteger(items.size());
        MicrogridStorageRunningModeRes webRes = new MicrogridStorageRunningModeRes();

        for (MicrogridStorageRunningModeReq.Item item : items) {
            MicrogridStorageRunningModeRes.Item webResItem = new MicrogridStorageRunningModeRes.Item().setId(item.getId());
            webRes.getItems().add(webResItem);
            String nodeId = deviceIdNodeIdMap.get(item.getId());
            if (nodeId == null || nodeId.isEmpty()) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                switch (item.getRunningMode()) {
                    case "1":
                        mqttClients.sendStorageDcOnReq(nodeId).setCallBack((success, res) -> {
                            if (success) {
                                webResItem.setRunningMode(res.getDcOn().equals("1") ? "1" : "");
                            }
                            if (countLatch.decrementAndGet() == 0) {
                                result.setResult(WebBaseResponse.success(webRes));
                            }
                        });
                        break;
                    case "2":
                        mqttClients.sendStorageDcOffReq(nodeId).setCallBack((success, res) -> {
                            if (success) {
                                webResItem.setRunningMode(res.getDcOff().equals("1") ? "2" : "");
                            }
                            if (countLatch.decrementAndGet() == 0) {
                                result.setResult(WebBaseResponse.success(webRes));
                            }
                        });
                        break;
                    case "3":
                        mqttClients.sendStorageClrFltReq(nodeId).setCallBack((success, res) -> {
                            if (success) {
                                webResItem.setRunningMode(res.getClrFlt().equals("1") ? "3" : "");
                            }
                            if (countLatch.decrementAndGet() == 0) {
                                result.setResult(WebBaseResponse.success(webRes));
                            }
                        });
                        break;
                }
            }
        }
    }

    // 储能 整机状态 设置
    public void storageMachineStateSet(DeferredResult<WebBaseResponse> result, List<MicrogridStorageMachineStateReq.Item> items) {
        if (items.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, String> deviceIdNodeIdMap = getDeviceIdNodeIdMap(items.stream().map(MicrogridStorageMachineStateReq.Item::getId).collect(Collectors.toList()));

        AtomicInteger countLatch = new AtomicInteger(items.size());
        MicrogridStorageMachineStateRes webRes = new MicrogridStorageMachineStateRes();

        for (MicrogridStorageMachineStateReq.Item item : items) {
            MicrogridStorageMachineStateRes.Item webResItem = new MicrogridStorageMachineStateRes.Item().setId(item.getId());
            webRes.getItems().add(webResItem);
            String nodeId = deviceIdNodeIdMap.get(item.getId());
            if (nodeId == null || nodeId.isEmpty()) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendPcsStorageMachineStateReq(nodeId, item.getMachineState()).setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setId(item.getId()).setMachineState(res.getMachineState());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }

    // 储能 设备侧功率 召测
    public void storageWGet(DeferredResult<WebBaseResponse> result, List<BigDecimal> deviceIds) {
        if (deviceIds.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, String> deviceIdNodeIdMap = getDeviceIdNodeIdMap(deviceIds);

        AtomicInteger countLatch = new AtomicInteger(deviceIds.size());
        MicrogridStorageWSetRes webRes = new MicrogridStorageWSetRes();

        for (BigDecimal deviceId : deviceIds) {
            MicrogridStorageWSetRes.Item webResItem = new MicrogridStorageWSetRes.Item().setId(deviceId);
            webRes.getItems().add(webResItem);
            String nodeId = deviceIdNodeIdMap.get(deviceId);
            if (nodeId == null || nodeId.isEmpty()) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendStorageWSetReq(nodeId, "").setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setW(res.getW());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }

    // 储能 设备侧功率 设置
    public void storageWSet(DeferredResult<WebBaseResponse> result, List<MicrogridStorageWSetReq.Item> items) {
        if (items.isEmpty()) {
            result.setResult(WebBaseResponse.errorDeviceIds());
        }
        Map<BigDecimal, String> deviceIdNodeIdMap = getDeviceIdNodeIdMap(items.stream().map(MicrogridStorageWSetReq.Item::getId).collect(Collectors.toList()));

        AtomicInteger countLatch = new AtomicInteger(items.size());
        MicrogridStorageWSetRes webRes = new MicrogridStorageWSetRes();

        for (MicrogridStorageWSetReq.Item item : items) {
            MicrogridStorageWSetRes.Item webResItem = new MicrogridStorageWSetRes.Item().setId(item.getId());
            webRes.getItems().add(webResItem);
            String nodeId = deviceIdNodeIdMap.get(item.getId());
            if (nodeId == null || nodeId.isEmpty()) {
                if (countLatch.decrementAndGet() == 0) {
                    result.setResult(WebBaseResponse.success(webRes));
                }
            } else {
                mqttClients.sendStorageWSetReq(nodeId, item.getW()).setCallBack((success, res) -> {
                    if (success) {
                        webResItem.setId(item.getId()).setW(res.getW());
                    }
                    if (countLatch.decrementAndGet() == 0) {
                        result.setResult(WebBaseResponse.success(webRes));
                    }
                });
            }
        }
    }
}
